package com.dna.common.io.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import junit.framework.TestCase;

import com.dna.common.io.MultiStreamJack;
import com.dna.common.io.StreamJack;


public class MultiStreamJackTestCase extends TestCase {

	private MultiStreamJack[] toDispose;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		if(toDispose!=null)
			for(int i=0; i < toDispose.length; i++)
				toDispose[i].close();
	}
	
	public void testMultiStreamJack() throws Exception {
		
		String message0 = "test message 0";
		byte[] msg0 = message0.getBytes();
		ByteArrayInputStream in0 = new ByteArrayInputStream(msg0); 
		
		String message1 = "test message 1";
		byte[] msg1 = message1.getBytes();
		ByteArrayInputStream in1 = new ByteArrayInputStream(msg1); 
		
		OutputStream output = new ByteArrayOutputStream();
		
		MultiStreamJack ms0 = new MultiStreamJack(StreamJack.DEFAULT_BUFFER_SIZE, new InputStream[]{in0, in1}, output);
		toDispose = new MultiStreamJack[]{ms0};
		ms0.autoFlush(true);
		ms0.start();
		while(ms0.isActive() && !ms0.isSuspend());
		String outStr = output.toString();
		for(int i = 0; i < outStr.length(); i++) {
			char c = outStr.charAt(i);
			assertTrue((message0.indexOf(c)>-1)||(message1.indexOf(c)>-1));
		}
	}
}
